﻿var App = function (win) {
    // IE mode
    var isRTL = false;
    var isIE8 = false;
    var isIE9 = false;
    var isIE10 = false;

    var handleInit = function () {
        if ($('body').css('direction') === 'rtl') {
            isRTL = true;
        }

        isIE8 = !!navigator.userAgent.match(/MSIE 8.0/);
        isIE9 = !!navigator.userAgent.match(/MSIE 9.0/);
        isIE10 = !!navigator.userAgent.match(/MSIE 10.0/);

        if (isIE10) {
            $('html').addClass('ie10'); // detect IE10 version
        }

        if (isIE10 || isIE9 || isIE8) {
            $('html').addClass('ie'); // detect IE10 version
        }
    };


    var initMiniui = function (precallback, endcallback) {
        $.ajax({
            beforeSend:function() {
                if (typeof precallback == "function") {
                    precallback();
                }  
            },
            url: "/Scripts/miniui/miniui.min.js",
            dataType: 'script',
            async:false,
            success: function () {
                $.ajax({
                    url: "/Scripts/com/ui-miniui.js",
                    dataType: 'script',
                    async: false,
                    success: function () {
                        $.mini.init();
                        if (typeof endcallback == "function") {
                            endcallback();
                        }
                    }
                });
            },
            error: function (ex) {
                console.log(ex);
            }
        });
    };

    var bindingViewModel = function (ovm) {
        if (!win.data) {
            win.data = {};
        }
        if (typeof win.beforeBind == "function") {
            win.beforeBind(ovm);
        }
        ko.applyBindings(ovm);
    }
    var initKnockout = function () {
        if (typeof win.preInit == "function") {
            win.preInit();
        }
        //执行模板处理
        if (typeof win.docLoad == "function") {
            win.docLoad();
        }
        //执行knockout初始化
        win.ovm = new viewModel(data);
        bindingViewModel(ovm);
        if (typeof win.docBind == "function") {
            win.docBind();
        }
    };

    return {
        init: function () {
            handleInit();
            initMiniui(function () {
                initKnockout();
            }, function () {
                //页面准备
                if (typeof win.docReady == "function") {
                    win.docReady();
                }
            });
        }
    };
}(window);


jQuery(document).ready(function () {
    App.init();
});